//
//  VideoTypeViewController.m
//  SnailPost
//
//  Created by Liuiu on 16/3/11.
//  Copyright © 2016年 抠泥ser. All rights reserved.
//

#import "VideoTypeViewController.h"
#import "VideoModel.h"
#import "VideoViewCell.h"
#import "PrefixHeader.pch"
#import <MediaPlayer/MediaPlayer.h>
#import <MJRefresh.h>

@interface VideoTypeViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableview; //实拍视频
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, assign)NSInteger number; //加载数据
@property (nonatomic, strong)NSIndexPath *indexPath; //视频专用
@property (nonatomic, strong)MPMoviePlayerViewController *movieView;// 按钮
@property (nonatomic, strong)UIButton *button; //按钮

@end

@implementation VideoTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    self.navigationItem.title = _hm.title;
    
    
    self.tableview = [[UITableView alloc]init];
    self.tableview.backgroundColor = [UIColor clearColor];
    _tableview.delegate = self;
    _tableview.dataSource = self;
   
    
    [self.view addSubview:_tableview];
    
    [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        
    }];
    
    //注册重用池
    [_tableview registerClass:[VideoViewCell class] forCellReuseIdentifier:@"VideoCell"];

    [self data];
    
    //上拉加载
    MJRefreshAutoNormalFooter *footer =[MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loading)];
    self.tableview.mj_footer = footer;
    
    //下拉加载时的字
    [footer setTitle:@"正在加载中..." forState:MJRefreshStateRefreshing];

    //加载初始变化值
    self.number = 0;
}

- (void)data
{
    NSString *str = [NSString stringWithFormat:@"http://c.m.163.com/nc/video/list/%@/y/0-10.html",_hm.sid];
    [LHAFNetworkTool getUrl:str body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        NSString *sta = _hm.sid;
        NSArray *arr = resposeObject[sta];
  
        self.dataArr = [VideoModel modelHanderWithArray:arr];
        [_tableview reloadData];
    
        NSLog(@"kkk%@",resposeObject);
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    

    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"VideoCell" forIndexPath:indexPath];
    VideoModel *hm =_dataArr[indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:hm.cover]placeholderImage:[UIImage imageNamed:@"mifeng"]];
    cell.title.text = hm.title;
    cell.content.text = hm.Description;
    cell.times.text = hm.length.stringValue;
    cell.counts.text = hm.playCount.stringValue;
    cell.backgroundColor = [UIColor clearColor];
    
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    cell.image.userInteractionEnabled = YES;
    [cell.image addGestureRecognizer:tap];
    
    return cell;
}
//点击图片触发的方法
-(void)tapAction:(UITapGestureRecognizer *)sender
{
    //点开新的关闭旧的
    if (self.movieView.view) {
        [self.movieView.view removeFromSuperview];
    }
    //点击的哪个ImageView
    UIView *view = sender.view;
    //imageView->contentView->cell
    UITableViewCell *cell = (UITableViewCell *)view.superview.superview;
    self.indexPath = [self.tableview indexPathForCell:cell];//获取被点击的cell的indexPath
    VideoModel *model = _dataArr[_indexPath.row];
    self.movieView = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:model.mp4_url]];
    self.movieView.view.frame = CGRectMake(-20, 75, self.view.frame.size.width + 40, [UIScreen mainScreen].bounds.size.height / 3.7);
    [self.movieView.moviePlayer setControlStyle:(MPMovieControlStyleEmbedded)];
    [self.movieView.moviePlayer setControlStyle:(MPMovieControlStyleEmbedded)];
    [cell addSubview:self.movieView.view];
    //    [_movieView release];
    //添加视频关闭按钮
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.button addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-cha"] forState:UIControlStateNormal];
    [self.movieView.view addSubview:self.button];
    //设置按钮的相对坐标
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.movieView.view).with.offset(-5);
        make.top.equalTo(self.movieView.view.mas_top).with.offset(5);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    //让选中的灰色状态栏消失掉
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 310;
}

//通知中心 让视频停掉
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.movieView.view) {
        //        self.movieView.moviePlayer.pause;
        [self.movieView.view removeFromSuperview];
    }
}

//实现上拉加载
- (void)loading
{
    _number +=10;
    //开始加载
    [_tableview.mj_footer beginRefreshing];
    
    
    [LHAFNetworkTool getUrl:[NSString stringWithFormat:@"http://c.m.163.com/nc/video/list/%@/y/%ld-10.html",_hm.sid, (long)_number] body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        NSString *stg = _hm.sid;
        NSArray *arr = resposeObject[stg];
        arr = [VideoModel modelHanderWithArray:arr];
        for (NSInteger i =0; i < arr.count; i++) {
            [_dataArr addObject:arr[i]];
        }
        
        [_tableview reloadData];
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    [self.tableview.mj_footer endRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
